package cake.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cake.bean.Cake;
import cake.db.MySqlDb;

//��Ʒ��ݿ���ʶ���
public class CakeDao {
	private MySqlDb db = new MySqlDb();

	//ͨ��id��ȡ��Ʒ
	public Cake getCake(long id) {
		Cake cake = null;
		String sql = "select * from Cake where id=" + id;
		try {
			ResultSet rs = db.query(sql);
			while (rs.next()) {
				cake = new Cake();
				cake.setId(rs.getLong("id"));
				cake.setCategoryId(rs.getLong("category"));
				cake.setName(rs.getString("name"));
				cake.setPicture(rs.getString("picture"));
				cake.setPrice(rs.getFloat("price"));
				cake.setRemark(rs.getString("remark"));
				cake.setValid(rs.getInt("valid"));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.Close();
		}

		return cake;
	}
	//��ȡ��¼����
	public int getTotalPage(String where)
	{
		String sql="select count(*) from Cake ";
		int total=0;
		if(!where.equals(""))
		{
			sql+=" Where "+where;
		}
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				total=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return total;
	}
	//��ҳ��ȡ��Ʒ�б?��pageSizeΪ0ʱ��ʾ����ҳ
	public List<Cake> getCakes(int currPage, int pageSize, String where) {
		List<Cake> cakes = new ArrayList<Cake>();
		String sql = "select * from Cake ";
		if (!where.equals("")) {
			sql += " Where " + where;
		}
		if(pageSize!=0)
		{
			sql+=" limit "+((currPage-1)*pageSize)+","+pageSize;
		}
		try {
			ResultSet rs = db.query(sql);
			while (rs.next()) {
				Cake cake = new Cake();
				cake.setId(rs.getLong("id"));
				cake.setCategoryId(rs.getLong("category"));
				cake.setName(rs.getString("name"));
				cake.setPicture(rs.getString("picture"));
				cake.setPrice(rs.getFloat("price"));
				cake.setRemark(rs.getString("remark"));
				cake.setValid(rs.getInt("valid"));

				cakes.add(cake);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.Close();
		}

		return cakes;
	}
	//���Ӳ�Ʒ
	public boolean addCake(Cake cake) {
		String sql = "insert into cake(name,price,remark,picture,category,valid) values('"
				+ cake.getName()
				+ "',"
				+ cake.getPrice()
				+ ",'"
				+ cake.getRemark()
				+ "','"
				+ cake.getPicture()
				+ "',"
				+ cake.getCategoryId() + ",1)";
		try {
			int row = db.execute(sql);

			return row > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.Close();
		}
		return false;

	}
	//�޸Ĳ�Ʒ
	public boolean updateCake(Cake cake) {
		Cake oldcake = getCake(cake.getId());

		if (oldcake == null) {
			return false;
		}

		String sql = "Update cake set name='" + cake.getName() + "',price="
				+ cake.getPrice() + ",picture='" + cake.getPicture()
				+ "',remark='" + cake.getRemark() + "',category="
				+ cake.getCategoryId() +",valid="+cake.getValid()
				+ " where id=" + cake.getId();

		try {
			int row = db.execute(sql);

			return row > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.Close();
		}
		return false;
	}
	//ɾ���Ʒ
	public boolean deleteCake(Cake cake) {
		Cake oldcake = getCake(cake.getId());

		if (oldcake == null) {
			return false;
		}

		String sql = "update Cake set valid=0 where id=" + cake.getId();

		try {
			int row = db.execute(sql);

			return row > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.Close();
		}
		return false;
	}
}
